package com.lsh.oms.core.service.order.ka;

import org.springframework.stereotype.Service;

/**
 * 订单发货service
 *
 * @author peter
 */
@Service
@Deprecated
public class OrderDeliveryKAService {

//    private static Logger logger = LoggerFactory.getLogger(OrderDeliveryKaService.class);
//
//    @Autowired
//    private ShippingOrderService shippingService;
//
//    @Autowired
//    private EsProxy esProxy;
//
//    @Autowired
//    private GoodsServiceProxy goodsServiceProxy;
//

    /**
     * 发货单
     *
     * @param orderShippingHeadKaDto 请求信息
     * @param orderHead              订单信息
     * @param logSign                唯一标识
     * @return 返回值
     */
//    @Transactional(rollbackFor = Exception.class)
//    public CommonResult<Object> createShippingOrder(OrderShippingHeadKaDto orderShippingHeadKaDto, OrderHead orderHead, String logSign) {
//        logger.info(logSign + "发货单请求发货参数" + JsonUtils.obj2Json(orderShippingHeadKaDto));
//        //TODO:发货单标记
//        if (StringUtils.isBlank(orderShippingHeadKaDto.getShippingOrderId())) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "请输入第三方发货单号.请查询");
//        }
//
//        for (OrderShippingDetailKaDto detail : orderShippingHeadKaDto.getShippingDetails()) {
//            if (detail == null || StringUtils.isBlank(detail.getCode()) || detail.getRealQty() == null || detail.getRealQty().compareTo(BigDecimal.ZERO) <= 0) {
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "明细中有没有文固码或者没有数量，请核实");
//            }
//        }
//
//        Long orderCode = orderHead.getOrderCode();
//        JSONObject extjson = JSON.parseObject(orderHead.getExt());
//        String providerId = extjson.getString("providerId");
//        if (StringUtils.isBlank(providerId)) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "订单信息中没有供商信息，请联系相关人员处理");
//        }
//        //请求商品服务
//        orderShippingHeadKaDto.setOrderId(orderCode);
//        Map<String, Good> codeMaps = goodsServiceProxy.requestCode2Goods(orderShippingHeadKaDto.getOrderId(), orderHead.getParentOrderCode(), providerId, orderHead.getVenderId(), logSign);
//        //请求计算服务
//        CalResultVo calVo = this.requestCount(codeMaps, orderShippingHeadKaDto, logSign, orderHead.getVenderId());
//        OrderShippingHeadWithBLOBs osh = OrderFactory.createShippingHead(orderShippingHeadKaDto, orderHead, calVo);
//        List<OrderShippingDetail> details = this.getOrderShippingDetails(osh, calVo, codeMaps);
//        if (osh.getSkuCount().compareTo(orderHead.getGoodsTotalQuantity()) > 0) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "发货单数量大于订单数量,请核实发货数据");
//        }
//
//        boolean successFlag;
//        try {
//            successFlag = false;
////                    this.shippingService.dealShipping(osh, details, logSign);
//        } catch (Throwable e) {
//            logger.info(logSign + "发货单保存数据库失败，订单号:" + orderCode);
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "发货单保存数据库失败");
//        }
//        //通知es
//        this.orderInfoNotifyEs4ka(orderCode, OrderFactory.getMarketName(orderHead), OrderFactory.getProviderId(orderHead), logSign);
//
//        CommonResult<Object> res = new CommonResult<>();
//        Map<String, Object> respMap = new HashMap<>();
//        respMap.put("flag", successFlag);
//        BigDecimal sourceMoney = new BigDecimal(calVo.getHead().getMoney());
//        BigDecimal saled_money = new BigDecimal(calVo.getHead().getDiscounted_money());
//        BigDecimal coupon_money = saled_money.subtract(sourceMoney);
//
//        respMap.put("real_pay_money", BigDecimal.ZERO);
//        respMap.put("money", sourceMoney);
//        respMap.put("total_coupon_money", coupon_money);
//        res.setCode(ReturnCode.REQUEST_SUCCESS);
//        res.setData(respMap);
//        return res;
//    }

//    /**
//     * 请求计算服务
//     *
//     * @param codeMaps               商品信息
//     * @param orderShippingHeadKaDto 发货信息
//     * @param logSign                唯一标识
//     * @return 计算结果
//     */
//    private CalResultVo requestCount(Map<String, Good> codeMaps, OrderShippingHeadKaDto orderShippingHeadKaDto, String logSign, Long venderId) {
//        //请求计算服务
//        for (OrderShippingDetailKaDto shippingDetail : orderShippingHeadKaDto.getShippingDetails()) {
//            Good good = codeMaps.get(shippingDetail.getCode());
//            if (good == null) {
//                logger.info(logSign + "【wg 商品不在此订单中，处理失败，商品码:{}】", shippingDetail.getCode());
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "wg 商品不在此订单中，处理失败，商品码:" + shippingDetail.getCode());
//            }
//            logger.info(logSign + JsonUtils.obj2Json(good));
//            shippingDetail.setItemId(good.getSkuCode());
//            shippingDetail.setSkuId(good.getGoodsCode());
//        }
//
//        CalResultVo calVo = ComputeServiceUtils.requsetCalService4ka(orderShippingHeadKaDto, logSign, calUrl, venderId);
//        logger.info(logSign + "【请求计算服务返回信息】{}", JsonUtils.obj2Json(calVo));
//        if (calVo == null || calVo.getHead() == null || calVo.getDetail() == null || calVo.getDetail().size() == 0) {
//            logger.info(logSign + ",【计算服务获取信息为空,订单号】:" + orderShippingHeadKaDto.getOrderId());
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "计算服务获取信息为空");
//        }
//
//        return calVo;
//    }


//    /**
//     * 组织签收单详情列表
//     *
//     * @param osh      签收单详情
//     * @param calVo    计算服务数据
//     * @param codeMaps 商品服务数据
//     * @return 返回数据
//     */
//    private List<OrderShippingDetail> getOrderShippingDetails(OrderShippingHeadWithBLOBs osh, CalResultVo calVo, Map<String, Good> codeMaps) {
//        List<CalShippingDetailVo> detailVoList = calVo.getDetail();
//        List<OrderShippingDetail> details = new ArrayList<>(detailVoList.size());
//        String wgCode;
//        int sysTime = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
//        for (CalShippingDetailVo detailVo : detailVoList) {
//            OrderShippingDetail detail = new OrderShippingDetail(detailVo);
//            detail.setShippingOrderId(osh.getShippingOrderId());
//            String ext = detailVo.getExt();
//            if (StringUtils.isBlank(ext)) {
//                logger.info("计算服务返回");
//                return Collections.EMPTY_LIST;
//            }
//
//            JSONObject detailExt = JSONObject.parseObject(ext);
//            String isWeighingGoods = detailExt.getString("is_weighing_goods");
//
//            if (StringUtils.isBlank(isWeighingGoods)) {
//                isWeighingGoods = "0";
//            }
//
//            detailExt.put("is_weighing_goods", isWeighingGoods);
//
//            detail.setExt(detailExt.toJSONString());
//            wgCode = detailExt.getString("wm_code");
//
//            String skuCode = detailVo.getSkuId();
//            logger.info("计算服务skuCode is" + skuCode);
//            logger.info("商品服务codeMaps is" + JSON.toJSONString(codeMaps));
//            detail.setQty(new BigDecimal(detailVo.getQty()));
//            Good good = codeMaps.get(wgCode);
//            detail.setItemId(good.getSkuCode());
//            detail.setCode(good.getSupplySkuCode());
//            detail.setCreatedAt(sysTime);
//            detail.setUpdatedAt(sysTime);
//            detail.setZoneId(osh.getZoneId());
//            detail.setSupplierOrg(good.getSupplierOrg());
//            // TODO 2019-09-10 venderId
//            detail.setVenderId(osh.getVenderId());
//            // 2019-07-10 peter
//            Integer itemCode = detailVo.getItemCode();
//            if (null == itemCode) {
//                itemCode = 0;
//            }
//            detail.setItemCode(itemCode);
//            details.add(detail);
//        }
//
//        return details;
//    }

//    /**
//     * 通知ES
//     *
//     * @param orderCode 订单号
//     * @param logSign   唯一标识
//     */
//    private void orderInfoNotifyEs4ka(Long orderCode, String marketName, String providerId, String logSign) {
//        try {
//            OrderShippingHeadWithBLOBs shippingEs = shippingService.getBolbs(orderCode);
//
//            logger.info(logSign + "wg-ES入参新建发货索引参数是:" + JsonUtils.obj2Json(shippingEs));
//            shippingEs.setMarketName(marketName);
//            shippingEs.setProviderId(providerId);
//            String addressInfo = shippingEs.getAddressInfo();
//
//            if (StringUtils.isNotBlank(addressInfo)) {
//                JSONObject address = JSON.parseObject(addressInfo);
//                shippingEs.setAddressTelephone(address.getString("contact_phone"));
//            }
//
//            logger.info(logSign + "wg-ES入参新建发货索引参数all是:" + JsonUtils.obj2Json(shippingEs));
//            esProxy.createIndex(shippingEs, logSign);
//            OrderHeadForEs ohEs = new OrderHeadForEs();
//            ohEs.setOrderCode(orderCode);
//            ohEs.setOrderStatus(OrderStatus.ORDER_DELIVERY.getIndex());
//            logger.info(logSign + "wg-ES入参发货单是:" + JsonUtils.obj2Json(ohEs));
//            esProxy.editIndex(ohEs, logSign);
//        } catch (Exception e) {
//            logger.info(logSign + "订单号" + orderCode + ",生成发货单同步es失败。");
//            logger.info(e.getMessage(), e);
//        }
//    }

}
